import 'package:cached_network_image/cached_network_image.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:marquee/marquee.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/utils/utils.dart';

import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/user.dart';
import 'base/wqs_text.dart';

class ScrollBoard extends StatefulWidget {
  const ScrollBoard(
      {super.key,
      required this.type,
      this.margin,
      this.resource,
      this.canTap = true,
      this.color = Colours.white,
      this.gradient});

  final String type;
  final List<ResourceEntity>? resource;
  final EdgeInsets? margin;
  final bool canTap;
  final Color color;
  final Gradient? gradient;

  @override
  State<ScrollBoard> createState() => _ScrollBoardState();
}

class _ScrollBoardState extends State<ScrollBoard> {
  late List<ResourceEntity> data = widget.resource ?? [];

  @override
  void initState() {
    if (data.isEmpty) {
      getData();
    }
    super.initState();
  }

  Future getData() async {
    var result = await OtherApi.getAppList("app_announce") ?? [];
    data = result.where((element) => element.button == widget.type).toList();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return data.isEmpty
        ? Container()
        : Container(
            height: 30,
            margin: widget.margin,
            padding: const EdgeInsets.all(7),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(4),
                color: widget.gradient != null ? null : widget.color,
                gradient: widget.gradient),
            child: _car());
  }

  Widget _car() {
    return CarouselSlider(
      options: CarouselOptions(
        scrollPhysics: const NeverScrollableScrollPhysics(),
        viewportFraction: 1,
        scrollDirection: Axis.vertical,
        enableInfiniteScroll: (data.length) <= 1 ? false : true,
        autoPlay: (data.length) <= 1 ? false : true,
        onScrolled: (value) {},
      ),
      items: [
        ...List.generate(data.length, (i) {
          ResourceEntity? lbt = data[i];
          return GestureDetector(
              onTap: () {
                if (!widget.canTap) return;
                if (lbt.href.hasValue == true) {
                  if (lbt.needLogin == 1) {
                    User.needLogin(() {
                      Utils.doJump(lbt.href!);
                    });
                  } else {
                    Utils.doJump(lbt.href!);
                  }
                }
              },
              child: Row(
                children: [
                  lbt.imgUrl.hasValue == true
                      ? CachedNetworkImage(
                          width: 16,
                          height: 16,
                          imageUrl: lbt.imgUrl ?? "",
                          errorWidget: (context, url, error) => Container(),
                          placeholder: (context, url) => Container(),
                        )
                      : Image.asset(Utils.getImgPath("scroll_announce.png"),
                          width: 16),
                  Container(width: 4),
                  Expanded(
                    child: (lbt.title?.length ?? 0) > 25
                        ? Marquee(
                            text: lbt.title ?? "",
                            style: TextStyle(
                                fontSize: 12,
                                color: widget.gradient != null
                                    ? Colours.white
                                    : Colours.text292D32),
                            blankSpace: 10,
                            pauseAfterRound: const Duration(seconds: 1),
                          )
                        : QsText(lbt.title ?? "",
                            needCenter: true,
                            fontSize: 12,
                            color: Colours.text292D32,
                            maxLines: 1),
                  ),
                  if (widget.canTap && lbt.href.hasValue)
                    Image.asset(Utils.getImgPath("arrow_right_36.png"),
                        width: 12)
                ],
              ));
        })
      ],
    );
  }
}
